import { ConditionAll, ConditionList, ExtIdTuple, Page, Sortable } from "qqlx-core";
import { getPageDto } from "@lib/com.search.data";
import { getConditionMatchBool, getConditionSort } from "@lib/com.search.condition";
import { $request, CommonRequest } from "@lib/com.request";

export const HOST_DEVELOPMENT = "https://qqlx.ltd";

type ViewData<T> = {
  primaryMessage: string;

  schema: T;
  schemaList: T[];
  entity: T;
  entityList: T[];

  schemaLoading: boolean;
  schemaListLoading: boolean;
  entityLoading: boolean;
  entityListLoading: boolean;

  schemaDialog: boolean;
  schemaListDialog: boolean;
  entityDialog: boolean;
  entityListDialog: boolean;

  entityListPage: Page;
  entityListTotal: number;
  conditionList: ConditionList<T>;
};

export interface ViewBaseAction<T> {
  /** 默认 Time/Disabled */
  insertDefaultCondition: () => void;

  /** 默认 setSchema/Entity >>> clearPending >>> page=1 */
  initial: () => boolean;

  setSchema: (entity?: T) => boolean;
  setEntity: (entity?: T) => boolean;

  getSchema: () => T;
}

export class ViewBase<T extends Omit<ExtIdTuple, "id">> implements ViewBaseAction<T> {
  $request: CommonRequest = $request;
  setRequest(request: CommonRequest) {
    this.$request = request;
  }

  data: ViewData<T> = {
    primaryMessage: "正在加载",

    schemaLoading: true,
    schemaListLoading: true,
    entityLoading: true,
    entityListLoading: true,

    schemaDialog: false,
    schemaListDialog: false,
    entityDialog: false,
    entityListDialog: false,

    schema: {} as T,
    schemaList: [],
    entity: {} as T,
    entityList: [],

    entityListPage: getPageDto(),
    entityListTotal: 0,
    conditionList: [],
  };

  /** 启动四个loading */
  pending() {
    this.data.schemaLoading = true;
    this.data.schemaListLoading = true;
    this.data.entityLoading = true;
    this.data.entityListLoading = true;
  }

  /** 清除四个loading */
  clearPending() {
    this.data.schemaLoading = false;
    this.data.schemaListLoading = false;
    this.data.entityLoading = false;
    this.data.entityListLoading = false;
  }

  /** 清除四个dialog */
  clearDialog() {
    this.data.schemaDialog = false;
    this.data.schemaListDialog = false;
    this.data.entityDialog = false;
    this.data.entityListDialog = false;
  }

  /** 删除一个条件，再插入一个条件 */
  insertCondition(condition: ConditionAll<T>) {
    this.removeCondition(condition);
    this.data.conditionList.push(condition);
  }

  /** 根据 key 和 type 删除一个条件 */
  removeCondition(condition: ConditionAll<T>) {
    const index = this.data.conditionList.findIndex((e) => {
      return e.key === condition.key && e.type === condition.type;
    });
    if (index > -1) this.data.conditionList.splice(index, 1);
  }

  // ============ Interface ============
  // ============ Interface ============
  // ============ Interface ============

  initial() {
    this.setSchema();
    this.setEntity();
    this.clearPending();
    this.clearDialog();
    this.data.entityListPage.page = 1;
    return true;
  }

  insertDefaultCondition() {
    this.data.conditionList = [getConditionSort<T>("timeCreate" as any, Sortable.DESC), getConditionMatchBool<T>("isDisabled" as any, false)];
  }

  setSchema(entity?: T) {
    this.data.schema = entity ? structuredClone(entity) : this.getSchema();
    this.data.schemaList = [];
    return true;
  }

  setEntity(entity?: T) {
    this.data.entity = entity ? structuredClone(entity) : this.getSchema();
    return true;
  }

  getSchema(): T {
    return {} as T;
  }
}
